package org.semanticflash.rdf.io.ntriple
{
	
	
	import flexunit.framework.TestCase;
	
	import org.semanticflash.rdf.values.IRI;
	import org.semanticflash.rdf.values.RDFLangLiteral;
	import org.semanticflash.rdf.values.RDFPlainLiteral;
	import org.semanticflash.rdf.values.RDFTypedLiteral;
	import org.semanticflash.rdf.values.RDFValue;



	/**
	 * 
	 * Testing internals ( not the actual output )
	 * 
	 * @author aleyton@gmail.com
	 * 
	 */
	public class NTripleParserInternalTest extends TestCase
	{
		
		
		////////////////////////////////////////// Utils ///////////////////////////////////////////////
		public function test_getIndexOfFirstNonWS():void
		{
			var ws1:String = "                 Hello World!";
			assertEquals( NTripleParser.getIndexOfFirstNonWS( ws1 ), ws1.indexOf( "H" ) );
			var ws2:String = "                 Hello World!                  ";
			assertEquals( NTripleParser.getIndexOfFirstNonWS( ws2 ), ws2.indexOf( "H" ) );			
			var ws3:String = "               \t  Hello World!                  \t";
			assertEquals( ws3.substring( NTripleParser.getIndexOfFirstNonWS( ws3 ) ), "Hello World!                  \t" );
		}
		
		
		public function test_getIndexOfLastNonWSBeforeDot():void
		{
			var ws1:String = "Hello World! \t.";
			assertEquals( NTripleParser.getIndexOfLastNonWSBeforeDot( ws1 ), ws1.indexOf( "!" ) );
			var ws2:String = "\t\n\\n\\Hello World!                           .                        \t";
			assertEquals( NTripleParser.getIndexOfLastNonWSBeforeDot( ws2 ), ws2.indexOf( "!" ) );
			var ws3:String = "Hello World! .";
			assertEquals( ws3.substring( 0, NTripleParser.getIndexOfLastNonWSBeforeDot( ws3 ) + 1 ), "Hello World!" );
		}
		public function test_findIRIInBrackets():void
		{
			var ws1:String = "<a>";
			assertEquals( NTripleParser.findIRIInBrackets( ws1 ), IRI.instance( "a" ) );
			var ws2:String = "<a\n\\n\r\\r\t\\t\"\\_:\u20ac?////>";
			assertEquals( NTripleParser.findIRIInBrackets( ws2 ), IRI.instance( "a\n\\n\r\\r\t\\t\"\\_:\u20ac?////" ) );
		}
		public function test_findDatatypeFromLastCaret():void
		{
			var ws1:String = "\"Hello World!\"^^<a>";
			assertEquals( NTripleParser.findDatatypeFromLastCaret( ws1, 15 ), IRI.instance( "a" ) );
			var ws2:String = "\"<a\n\r\t\\_:\u20ac?////>\"^^<a>";
			assertEquals( NTripleParser.findDatatypeFromLastCaret( ws2, 18 ), IRI.instance( "a" ) );
		}
		
		////////////////////////////////////////// Parsers /////////////////////////////////////////////
		public function test_parseSubject():void
		{
			assertEquals( NTripleParser.parseSubject( "<http://example.org/resource2>" ), IRI.instance( "http://example.org/resource2" ) );
			
			var bNode:IRI = NTripleParser.parseSubject( "_:anon" );
			assertTrue( bNode.asNamespace.toString().charAt( 0 ) == "_" );
		}
		public function test_parsePredicate():void
		{
			assertEquals( NTripleParser.parsePredicate( "<http://example.org/resource2>" ), IRI.instance( "http://example.org/resource2" ) );
		}
		public function test_parseObject():void
		{
			assertEquals( NTripleParser.parseObject( "<http://example.org/resource2>" ), IRI.instance( "http://example.org/resource2" ) );
			
			var bNode:RDFValue = NTripleParser.parseObject( "_:anon" );
			assertTrue( ( bNode as IRI ).stringForm.charAt( 0 ) == "_" );
			
			var pLit:RDFPlainLiteral = NTripleParser.parseObject( "\"Hello World!\"" ) as RDFPlainLiteral;
			assertEquals( pLit, RDFPlainLiteral.instance( "Hello World!" ) );
			
			var pLangLit:RDFLangLiteral = NTripleParser.parseObject( "\"Hello World!\"@en" ) as RDFLangLiteral;
			assertEquals( pLangLit , RDFLangLiteral.instance( "Hello World!", "en" ) );
			
			var tLit:RDFTypedLiteral = NTripleParser.parseObject( "\"Hello World!\"^^<a?user=me@home.com>" ) as RDFTypedLiteral;
			assertEquals( tLit, RDFTypedLiteral.instance( "Hello World!", IRI.instance( "a?user=me@home.com" ) ) );
			
			assertEquals( NTripleParser.parseObject( "\"backslash:\\\"" )	, RDFPlainLiteral.instance( "backslash:\\" ) );
			assertEquals( NTripleParser.parseObject( "\"dquote:\"\"" )		, RDFPlainLiteral.instance( "dquote:\"" ) );
			assertEquals( NTripleParser.parseObject( "\"newline:\n\"" )		, RDFPlainLiteral.instance( "newline:\n" ) );
			assertEquals( NTripleParser.parseObject( "\"return\r\"" )		, RDFPlainLiteral.instance( "return\r" ) );
			assertEquals( NTripleParser.parseObject( "\"tab:\t\"" )			, RDFPlainLiteral.instance( "tab:\t" ) );
			assertEquals( NTripleParser.parseObject( "\"x\"" )				, RDFPlainLiteral.instance( "x" ) );
			assertEquals( NTripleParser.parseObject( "\"_:anon\"" )			, RDFPlainLiteral.instance( "_:anon" ) );
			assertEquals( NTripleParser.parseObject( "\"\u00E9\"" )			, RDFPlainLiteral.instance( "\u00E9" ) );
			assertEquals( NTripleParser.parseObject( "\"\u20AC\"" )			, RDFPlainLiteral.instance( "\u20AC" ) );
			assertEquals( NTripleParser.parseObject( "\"\"" )				, RDFPlainLiteral.instance( "" ) );
			assertEquals( NTripleParser.parseObject( "\" \"" )				, RDFPlainLiteral.instance( " " ) );
			assertEquals( NTripleParser.parseObject( "\"\"\"" )				, RDFPlainLiteral.instance( "\"" ) );
			assertEquals( NTripleParser.parseObject( "\"<a></a>\"" )		, RDFPlainLiteral.instance( "<a></a>" ) );
			assertEquals( NTripleParser.parseObject( "\"a <b></b>\"" )		, RDFPlainLiteral.instance( "a <b></b>" ) );
			assertEquals( NTripleParser.parseObject( "\"a <b></b> c\"" )	, RDFPlainLiteral.instance( "a <b></b> c" ) );
			assertEquals( NTripleParser.parseObject( "\"a\n<b></b>\n\"" )	, RDFPlainLiteral.instance( "a\n<b></b>\n" ) );
			assertEquals( NTripleParser.parseObject( "\"chat\"" )			, RDFPlainLiteral.instance( "chat" ) );
			assertEquals( NTripleParser.parseObject( "\"chat\"" )			, RDFPlainLiteral.instance( "chat" ) );
			assertEquals( NTripleParser.parseObject( "\"chat\"" )			, RDFPlainLiteral.instance( "chat" ) );
			assertEquals( NTripleParser.parseObject( "\"abc\"" )			, RDFPlainLiteral.instance( "abc" ) );
		}
	}
}